package com.mgface.eth.api;

import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.RemoteCall;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.Transfer;
import org.web3j.utils.Convert;

import java.math.BigDecimal;

/**
 * @author wanyuxiang
 * @version 1.0
 * @project eth
 * @desc 转账以太币
 * @create 2018-06-26 22:18
 **/
public class TransferEth {
    public static void  transfer(String privateKey,String toAddress) throws Exception {
        Web3j web3 = Web3j.build(new HttpService("https://ropsten.infura.io/IY77yuopq4bSgHoaFqJe"));
        Credentials credentials = Credentials.create(privateKey);
        RemoteCall<TransactionReceipt> call = Transfer.sendFunds(web3,credentials,toAddress, BigDecimal.valueOf(1.0), Convert.Unit.ETHER);
        TransactionReceipt receipt =  call.send();
        System.out.println(receipt);
        String txHash = receipt.getTransactionHash();
        System.out.println(txHash);
        //TransactionReceipt transactionReceipt = SendTx.waitForTransactionReceipt(txHash);
        //System.out.println(transactionReceipt);
    }

    public static void main(String[] args)  throws Exception {
        transfer("8341e5451480e81f220610f9ada693fe7a5dcc19d28833ce0c2f4f4d7f649104",
                "0xf3E8F0B497B6eacD3Ae405C4011CFe7a9CebEeB2");
    }
}
